<?php

namespace App\model;

use App\Model\UserModel;
use App\Model\StoreGoodModel;
use App\Model\ShopOrderFstModel;
use Illuminate\Database\Eloquent\Model;
//首页数据统计
class HomeTotalModel extends Model
{

	protected $table = 'home_total';

	protected $guarded = [];

	function getTrueAttribute($value)
	{
		$start = date('Y-m-d 00:00:00');
		$end = date('Y-m-d 23:59:59');
		switch($this->id){
			case '1':
				return UserModel::count();
				break;
			case '2':
				//总入库量
				return StoreGoodModel::sum('num');
				break;
			case '3':
				//总销量(元)
				return ShopOrderFstModel::sum('total');
				break;
			case '4':
				//今日新增会员
				return UserModel::where('created_at',[$start,$end])->count();
				break;
			case '5':
				//今日入库量
				return StoreGoodModel::where('created_at',[$start,$end])->sum('num');
				break;
			case '6':
				//今日销量(元)
				return ShopOrderFstModel::where('created_at',[$start,$end])->sum('total');
				break;
		}
	}

	static function doList(){
		$data['user_sum'] = HomeTotalModel::config(1);
		$data['store_sum'] = HomeTotalModel::config(2);
		$data['sell_sum'] = HomeTotalModel::config(3);
		$data['new_add_sum'] = HomeTotalModel::config(4);
		$data['new_store_sum'] = HomeTotalModel::config(5);
		$data['new_sell_sum'] = HomeTotalModel::config(6);

		return $data;
	}

	static function config($id){
		$start = date('Y-m-d 00:00:00');
		$end = date('Y-m-d 23:59:59');
		$data = HomeTotalModel::where('id',$id)->first();
		if($data['state'] == 2){
			switch($id){
				case '1':
					return UserModel::count();
					break;
				case '2':
					//总入库量
					return StoreGoodModel::sum('num');
					break;
				case '3':
					//总销量(元)
					return ShopOrderFstModel::sum('total');
					break;
				case '4':
					//今日新增会员
					return UserModel::whereBetween('created_at',[$start,$end])->count();
					break;
				case '5':
					//今日入库量
					return StoreGoodModel::whereBetween('created_at',[$start,$end])->sum('num');
					break;
				case '6':
					//今日销量(元)
					return ShopOrderFstModel::whereBetween('created_at',[$start,$end])->sum('total');
					break;
			}
		}else{
			return $data['value'];
		}

	}





}